//
//  YFWelcomeView.swift
//  YFSwiftWeibo
//
//  Created by tsaievan on 2017/9/5.
//  Copyright © 2017年 tsaievan. All rights reserved.
//

import UIKit
import SDWebImage

class YFWelcomeView: UIView {
    
    @IBOutlet weak var iconImageView: UIImageView!
    @IBOutlet weak var tipLabel: UILabel!
    ///< 底部的约束
    @IBOutlet weak var bottomCons: NSLayoutConstraint!
    
    class func welcomeView()->YFWelcomeView {
        let nib = UINib.init(nibName: "YFWelcomeView", bundle: nil)
        
        ///< xib一定要关联对应的类, 下面强转才可以, 否则一强转就崩
        let v = nib.instantiate(withOwner: nil, options: nil)[0] as! YFWelcomeView
        v.frame = UIScreen.main.bounds
        return v 
    }
    
    override func awakeFromNib() {
        super.awakeFromNib()
        iconImageView.layer.cornerRadius = iconImageView.bounds.size.width * 0.5
        iconImageView.layer.masksToBounds = true
        
        guard let urlString = YFNetworkManager.shared.tokenModel.avatar_large else{
            return
        }
        let url = URL(string: urlString)
            
        iconImageView.sd_setImage(with: url, placeholderImage: (#imageLiteral(resourceName: "avatar_default_big")))
    }

}

extension YFWelcomeView {
    
    /// 已经添加到窗口上调用
    override func didMoveToWindow() {
        ///< 调用一下super先
        super.didMoveToWindow()
        layoutIfNeeded()
        
        bottomCons.constant = UIScreen.hm_screenHeight() - 200
    
        UIView.animate(withDuration: 2, delay: 0, usingSpringWithDamping: 0.5, initialSpringVelocity: 0.5, options: [], animations: {
            self.layoutIfNeeded()
        }) { (_) in
            UIView.animate(withDuration: 2, animations: { 
                self.tipLabel.alpha = 1
            }, completion: { (_) in
                ///< 动画完成后, 从父控件上移除
                self.removeFromSuperview()
            })
        }
    }
}
